//
// Created by tcj on 2022/3/31.
//
#include "sylar/http/http_parser.h"
#include "sylar/log.h"

static sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();

const char test_request_data[] = "GET / HTTP/1.1\r\n"
                                 "Host: www.syklar.top\r\n"
                                 "Content-Length:10\r\n\r\n"
                                 "1234567890";

void test_request(){
    sylar::http::HttpRequestParser parser ;
    std::string tmp = test_request_data;
    size_t s = parser.execute(&tmp[0],tmp.size());
    SYLAR_LOG_INFO(g_logger)<<"execute rt=" << s
                <<" has_error="<<parser.hasError()
                <<" is_finished="<<parser.isFinished()
                <<" total size="<<tmp.size()
                << " content_length="<<parser.getContentLength();
    SYLAR_LOG_INFO(g_logger)<<parser.getData()->toString();
    SYLAR_LOG_INFO(g_logger)<<tmp;
}

const char test_reponse_data[] = "";
void test_reponse(){
    sylar::http::HttpResponseParser::ptr parser;
    std::string tmp = test_reponse_data;
    size_t s = parser->execute(&tmp[0],tmp.size());
    SYLAR_LOG_ERROR(g_logger)<<"execute rt="<<s
            <<" has_error="<<parser->hasError()
            <<" is_finished="<<parser->isFinished()
            <<" total="<<tmp.size()
            <<" content-length="<<parser->getContentLength()
            <<" tmp[s]="<<tmp[s];

    tmp.resize(tmp.size()-s);
    SYLAR_LOG_INFO(g_logger)<<parser->getData()->toString();
    SYLAR_LOG_INFO(g_logger)<<tmp;
}

int main(){
    test_request();
    SYLAR_LOG_INFO(g_logger)<<"---------------------------";
    test_reponse();
    return 0;
}
